package contoller

import (
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"go_demo_1/common"
	"go_demo_1/conf"
	"go_demo_1/model/menu"
	"net/http"
)

//var Db *gorm.DB
//
//func init()  {
//	fmt.Println(Db)
//	if(nil == Db){
//		Db = common.Db
//	}
//}
func Index(c *gin.Context) {
	//menu.CreateTable()//检查数据结构，不存在就主动调用，进行创建方法调用
	session := sessions.Default(c)

	if session.Get("hello") != "world" {
		session.Set("hello", "world-1")
		session.Save()
	}
	var menus []menu.Menu
	common.Db.Find(&menus)

	for _, v := range menus {
		fmt.Println(v.Link)
	}

	common.LogInfo("我就是骂你了，怎么的", "logs/001.txt")
	err := common.Rdb.Set("score", 1000000, 0).Err()
	if err != nil {
		fmt.Printf("set score failed, err:%v\n", err)
		return
	}

	val, err := common.Rdb.Get("score").Result()
	if err != nil {
		fmt.Printf("get score failed, err:%v\n", err)
		return
	}

	fmt.Println(val)


	//cookie 的获取与设置
	cookie, err :=  c.Cookie("gin_cookie")
	if err != nil {
		fmt.Println(err)
		cookie = "NotSet"
		// 设置Cookie
	    //http: named cookie not present  出现的原因一般是因为作用域的问题，域名的问题造成的

		c.SetCookie("gin_cookie", "test", 3600, "/", "127.0.0.1:8888", false, false)
	}
	fmt.Printf("Cookie value: %s \n", cookie)



	c.HTML(http.StatusOK, "index.html", gin.H{
		"host":  conf.Conf.Http.Hostname,
		"menus": menus,
	})
}
